<!--
 * @Description: file content
 * @Author: Zt2tzzt
 * @Date: 2021-09-17 09:54:06
 * @LastEditors: ZeT1an
 * @LastEditTime: 2022-02-03 01:53:59
 * @LastEditContent: 
-->
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    .active {
      color: red;
    }
  </style>
</head>

<body>
  <div id="app"></div>

  <template id="my-app">
    <div :class="className">哈哈哈哈</div>

    <!-- 对象语法: {'className': boolean} / {className: boolean}  -->
    <div :class="{active: isActive}">哈哈哈哈</div>
    <button @click="toggle">切换</button>

    <!-- 也可以有多个键值对 -->
    <div :class="{active: isActive, title: true}">呵呵呵呵</div>

    <!-- 默认的class和动态的class结合 -->
    <div class="abc cba" :class="{active: isActive, title: true}">呵呵呵呵</div>

    <!-- 将对象放到一个单独的属性中 -->
    <div class="abc cba" :class="classObj">呵呵呵呵</div>

    <!-- 将返回的对象放到一个method(computed)方法中 -->
    <div class="abc cba" :class="getClassObj()">嘿嘿嘿嘿</div>

  </template>

  <script src="https://unpkg.com/vue@next"></script>
  <script>
    const app = {
      template: '#my-app',
      data() {
        return {
          className: 'zzt',
          isActive: true,
          classObj: {
            active: true,
            title: true
          }
        }
      },
      methods: {
        toggle() {
          this.isActive = !this.isActive
        },
        getClassObj() {
          return {
            active: true,
            title: true
          }
        }
      }
    }
    Vue.createApp(app).mount('#app')
  </script>
</body>

</html>